package ua.com.fesvlast.hotel.model.commands;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import ua.com.fesvlast.hotel.controller.managers.ConfigurationManager;
import ua.com.fesvlast.hotel.controller.managers.MessageManager;
import ua.com.fesvlast.hotel.model.dao.DAOFactory;
import ua.com.fesvlast.hotel.model.enteties.Contacts;
import ua.com.fesvlast.hotel.model.enteties.HotelRooms;
import ua.com.fesvlast.hotel.model.enteties.Orders;
import ua.com.fesvlast.hotel.model.enteties.Users;

public class AcceptOrderCommand implements ICommand {

	@Override
	public String execute(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String page=null;
		Users currentUser =(Users)request.getSession().getAttribute("currentUser");
		Orders order=null;
		HotelRooms room=null;
		Contacts contact=null;
		
		if(currentUser==null  || !currentUser.getUserType().equals("admin")){
			page=ConfigurationManager.getInstance().getProperty(ConfigurationManager.LOGIN_PAGE_PATH);
			return page;
		}
		
			int orderId =Integer.valueOf(request.getParameter("orderId"));
			order =DAOFactory.getOrdersDAO().getCurrentOrderByID(orderId);
			room =DAOFactory.getHotelsRoomsDAO().getChosenRoom(order.getHotelRoomId());
			contact=DAOFactory.getContactsDAO().getCurrentContactByID(order.getUserId());
				
		if(order!=null && room!=null && contact !=null){
			request.setAttribute("order", order);
			request.setAttribute("room", room);
			request.setAttribute("contact", contact);
			page=ConfigurationManager.getInstance().getProperty(ConfigurationManager.BILL_PAGE);
		}else{
			request.setAttribute("errorMessage", MessageManager.getInstance().getProperty(MessageManager.SERVLET_EXCEPTION_ERROR_MESSAGE));	
			page=ConfigurationManager.getInstance().getProperty(ConfigurationManager.ERROR_PAGE_PATH);
					
		}
		
		return page;
	}

}
